local IgnoreNotes = CreateFrame("Frame", "IgnoreNotesFrame");

IgnoreNotes:SetScript("OnEvent", function(self, event, ...)
	self[event](self, event, ...)
end)
IgnoreNotes:RegisterEvent("ADDON_LOADED");

local sv;

function IgnoreNotes.ADDON_LOADED(self, event, ...)
	local arg1 = ...
	if arg1 == "IgnoreNotes" then
		sv = IgnoreNotesPC or {};
	end		
end

local ignoreName;
local INMenuTable = {{text = "Edit Note", checked = nil, notCheckable = 1, func = function() StaticPopup_Show("IGNORE_NOTES_EDIT") end,},}
local INDropDownMenu = CreateFrame("Frame", "INMenuTable", nil, "UIDropDownMenuTemplate")

StaticPopupDialogs["IGNORE_NOTES_EDIT"] = {
		text = "Enter or edit an Ignore Note.",
		button1 = ACCEPT,
		button2 = CANCEL,
		hasEditBox = 1,
		maxLetters = 255,
-- 		hasWideEditBox = 1,
		editBoxWidth = 300,
		OnAccept = function(self)
			local note = self.editBox:GetText()
			if note and note ~= "" then
				sv[ignoreName] = note;
			else
				sv[ignoreName] = nil;
			end
			self:Hide();
			IgnoreNotesPC = sv;
		end,
		OnShow = function(self)
			local note = sv[ignoreName];
			if note and note ~= "" then
				self.editBox:SetText(note);
			end
			self.editBox:SetFocus();
		end,
		OnHide = function(self)
			self.editBox:SetText("");
		end,
		EditBoxOnEnterPressed = function(self)
			local parent = self:GetParent();
			local note = parent.editBox:GetText()
			if note and note ~= "" then
				sv[ignoreName] = note;
			else
				sv[ignoreName] = nil;
			end
			parent:Hide();
			IgnoreNotesPC = sv;
		end,
		EditBoxOnEscapePressed = function(self)
			self:GetParent():Hide();
		end,
		timeout = 0,
		exclusive = 1,
		whileDead = 1,
		hideOnEscape = 1	
}

do
	for i=1,IGNORES_TO_DISPLAY do
		local ignoreBtn = _G["FriendsFrameIgnoreButton"..i]
		ignoreBtn:HookScript("OnMouseUp", function(self, button) 
				if button == "RightButton" then
					ignoreName = GetIgnoreName(self.index);
					EasyMenu(INMenuTable, INDropDownMenu, self, 50, 0, "MENU", 3)
				end
			end)
		ignoreBtn:SetScript("OnEnter", function(self) 
				local iName = GetIgnoreName(self.index);
				local note = sv[iName]
				if note and note~="" then
					GameTooltip:SetOwner(self, "ANCHOR_TOPLEFT");
					GameTooltip:AddLine(iName);
					GameTooltip:AddLine(note, 1.0, 0.6, 0.0, 1);
					GameTooltip:Show();
				end
			end)
		ignoreBtn:SetScript("OnLeave", function(self) 
				GameTooltip:Hide();
			end)
	end
end
